package com.yu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.bean.Picture;
import com.yu.bean.Tpicture;
import com.yu.bean.TpictureExample;
import com.yu.bean.TpictureExample.Criteria;
import com.yu.dao.PictureMapper;
import com.yu.dao.TpictureMapper;

@Service
public class TpictureService {
	
	@Autowired
	TpictureMapper tpictureMapper;

	/**
	 * 获取信息标题图片
	 * @param infoId
	 * @return
	 */
	public  Tpicture getTpicureById(String infoId) {
		// TODO Auto-generated method stub
		TpictureExample tpictureExample = new TpictureExample();
		Criteria criteria = tpictureExample.createCriteria();
		criteria.andInfoIdEqualTo(Integer.parseInt(infoId));
	     List<Tpicture> tpic = tpictureMapper.selectByExample(tpictureExample);
	     if (tpic.size() != 0)
	    	 return  tpic.get(0);
	     else
	    	 return null;
	}

	/**
	 * 插入上传标题图片到标题数据库
	 * @param id
	 * @param storUrl
	 */
	public void uploadtImgs(String id, String storUrl) {
		// TODO Auto-generated method stub
		Tpicture tp =new Tpicture();
		tp.setInfoId(Integer.parseInt(id));
		tp.setTpicUrl(storUrl);
		tpictureMapper.insert(tp);
	}

	/**
	 * 当上传图片成功时立即把该图片对象返回页面
	 * @param storUrl
	 * @return
	 */
	public Tpicture selectTpicByUrl(String storUrl) {
		// TODO Auto-generated method stub
		TpictureExample tpictureExample = new TpictureExample();
		Criteria criteria = tpictureExample.createCriteria();
		criteria.andTpicUrlEqualTo(storUrl);
		List<Tpicture > tpics = tpictureMapper.selectByExample(tpictureExample);
		if(tpics.size() != 0)
			return tpics.get(0);
		else
			return null;
		
	}

	/**
	 * 删除标题图片
	 * @param dId
	 */
	public void delTpicById(Integer dId) {
		// TODO Auto-generated method stub
		tpictureMapper.deleteByPrimaryKey(dId);
		
	}

	/**
	 * 根据要批量删除的信息id 批量删除信息标题图片
	 * @param del_ids
	 */
	public void deleteTpicsByInfoIds(List<Integer> del_ids) {
		// TODO Auto-generated method stub
		TpictureExample tpictureExample = new TpictureExample();
		Criteria criteria = tpictureExample.createCriteria();
		criteria.andInfoIdIn(del_ids);
		tpictureMapper.deleteByExample(tpictureExample);
		
	}

	/**
	 * 根据要删除的信息删除信息标题图片
	 * @param id
	 */
	public void deleteTpicByInfoId(Integer id) {
		// TODO Auto-generated method stub
		TpictureExample tpictureExample = new TpictureExample();
		Criteria criteria = tpictureExample.createCriteria();
		criteria.andInfoIdEqualTo(id);
		tpictureMapper.deleteByExample(tpictureExample);
		
	}
	

}
